package com.sduept.nwld.mobile.manager;

import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.entity.FaultReportComponentDetail;
import com.sduept.bigdata.fault.entity.FaultReportStationDetail;
import com.sduept.bigdata.fault.manager.FaultWaveFileRelationManager;
import com.sduept.bigdata.fault.report.etl.factory.AnalysisModelFactoryBuilder;
import com.sduept.bigdata.fault.report.etl.model.FaultDetailDTO;
import com.sduept.bigdata.fault.report.etl.model.ProtectionActionAnalysisModel;
import com.sduept.cim.model.extend.AbstractWaveFile;
import com.sduept.cim.model.extend.ProtectWaveFile;
import com.sduept.cim.model.extend.WaveFile;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.mobile.manager.fault.FaultCharacteristicInfoManager;
import com.sduept.nwld.mobile.manager.fault.FaultRecordManager;
import com.sduept.nwld.mobile.manager.fault.FaultReportStationDetailManager;
import com.sduept.nwld.mobile.model.fault.*;
import com.sduept.nwld.wave.util.ProtectionNwUtil;
import com.sduept.permission.entity.DepartmentEntityBean;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.entity.Principal;
import com.sduept.permission.entity.RoleEntityBean;
import com.sduept.permission.enums.RoleTypeEnum;
import com.sduept.permission.manager.DepartManager;
import com.sduept.permission.manager.PersonnelManager;
import com.sduept.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**    
 *  
 * Simple to Introduction  
 *  
 * @Description:  [获取rest服务所需数据的manager]   
 * @Author:       [lixz]   
 * @CreateDate:   [2019年6月25日 下午1:06:48]   
 *  
 *    
 */

@Service
@Slf4j
public class FaultReportMobileManager {

	@Autowired
	private FaultRecordManager faultRecordManager;
	@Autowired
	private FaultReportStationDetailManager faultReportStationDetailManager;
	@Autowired
	private FaultCharacteristicInfoManager faultCharacteristicInfoManager;
	@Autowired
	private MobileUserInfoManager mu;
	@Autowired
	private DepartManager dm;
	@Autowired
	private PersonnelManager pm;
	@Autowired
	private FaultWaveFileRelationManager relationManager;
	@Autowired
	private SuperDAO dao;
	


	/**
	 * 根据年月查找一次设备 若只通过年查询，月份设置为0
	 * 
	 * @param currentYear
	 * @param currentMonth
	 * @return
	 */
	public int primFaultStatistic(int currentYear, int currentMonth) {
		return faultRecordManager.primFaultStatistic(currentYear, currentMonth);
	}

	/**
	 * 获取故障列表数据
	 * 
	 * @param month
	 * @param currentPage
	 * @param userCode
	 * @return
	 */
	public List<FaultListDTO> getFaultReportList(int month, int currentPage, String userCode) {
		if (month < 0 || month > 12 || currentPage < 0) {
			log.error("接口参数检查:month或者currentPage所传参数值不合法");
			return null;
		}
		int[] rangeArr = null;
		if (currentPage > 9) {
			rangeArr = new int[] { currentPage - 10, currentPage };
		} else if (currentPage > 0) {
			rangeArr = new int[] { 0, currentPage };
		}
		List<FaultRecord> faults = new ArrayList<>();
		if (month == 0) {
			// 最新
			if (StringUtils.isNotEmpty(userCode)) {
				Principal principal = getPrincipal(userCode);
				if(principal.isSystemManager()){
					//管理员权限，查询所有数据
					faults = faultRecordManager.findLastestGe500kVFaultForApp(null, rangeArr);
				}else {
					//不是管理员
					faults = faultRecordManager.findLastestGe500kVFaultForApp(principal, rangeArr);
				}
			} else {
				//无账号查询
				faults = faultRecordManager.findLastestGe500kVFaultForApp(null,rangeArr);
			}
		} else {
			Date startDate = null;
			Date endDate = null;
			Calendar calendar = Calendar.getInstance();
			int currentYear = calendar.get(Calendar.YEAR);
			int currentMonth = (calendar.get(Calendar.MONTH)) + 1;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
			if (month > currentMonth) {
				// 如果传入的月份大于当前月，就取上一年本月的
				currentYear -= 1;
			}
			// 当月第一天
			String firstDay = sdf1.format(DateUtil.getFirstDayOfMonth(currentYear, month));
			// 当月最后一天
			String lastDay = sdf1.format(DateUtil.getLastDayOfMonth(currentYear, month));
			try {
				startDate = sdf.parse(firstDay + " 00:00:00");
				endDate = sdf.parse(lastDay + " 23:59:59");
			} catch (ParseException e) {
				e.printStackTrace();
			}
			if (StringUtils.isNotEmpty(userCode)) {
				Principal principal = getPrincipal(userCode);
				if(principal.isSystemManager()){
					//管理员权限，查询所有数据
					faults = faultRecordManager.findGe500kVFaultByTimedForApp(null,startDate, endDate, rangeArr);
				}else {
					//不是管理员
					faults = faultRecordManager.findGe500kVFaultByTimedForApp(principal,startDate, endDate, rangeArr);
				}
			} else {
				//无账号查询
				faults = faultRecordManager.findGe500kVFaultByTimedForApp(null,startDate, endDate, rangeArr);
			}
		}
		return getFaultLists(faults);
	}

	/**
	 *  根据日期、设备名、厂站名、当前用户搜索故障列表数据
	 * @param startTime
	 * @param endTime
	 * @param deviceName
	 * @param stationName
	 * @param userCode
	 * @param currentPage
	 * @return
	 */
	public List<FaultListDTO> getFaultReportListByDateAndDeviceAndStationAndUserCode(String startTime,String endTime, String deviceName, String stationName, String userCode,String currentPage) {
		int[] rangeArr = null;
		if (StringUtils.isNotEmpty(currentPage)) {
			currentPage = currentPage.trim();
			//分页查询
			int currentPageInt = Integer.parseInt(currentPage);
			
			if (currentPageInt > 9) {
				rangeArr = new int[] { currentPageInt - 10, currentPageInt };
			} else if (currentPageInt > 0) {
				rangeArr = new int[] { 0, currentPageInt };
			}
		}
		//日期
		Date start = null;
		Date end = null;
		if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
			start = DateUtil.getDateByString(startTime + " 00:00:00", DateUtil.pattern_ymdhms);
			end = DateUtil.getDateByString(endTime + " 23:59:59", DateUtil.pattern_ymdhms);
		}
		if (StringUtils.isNotEmpty(deviceName)) {
			deviceName = deviceName.trim();
		}
		if (StringUtils.isNotEmpty(stationName)) {
			stationName = stationName.trim();
		}
		List<FaultRecord> faults = new ArrayList<>();
		if (StringUtils.isNotEmpty(userCode)) {
			//验证用户权限，然后在查询数据！
			Principal principal = getPrincipal(userCode);
			if(principal.isSystemManager()){
				//管理员权限，查询所有数据
				faults = getSearchfaultsDataBy(null,start, end, deviceName,stationName,rangeArr);
			}else {
				//不是管理员
				faults = getSearchfaultsDataBy(principal,start, end, deviceName,stationName,rangeArr);
			}
		} else {
			//无账号查询数据！
			faults = getSearchfaultsDataBy(null,start, end, deviceName,stationName,rangeArr);
		}
		return getFaultLists(faults);
	}
	/**
	 * 搜索查询的多种情况
	 * @param principal
	 * @param start
	 * @param end
	 * @param deviceName
	 * @param stationName
	 * @param rangeArr
	 * @return
	 */
	private List<FaultRecord> getSearchfaultsDataBy(Principal principal,Date start, Date end, String deviceName,String stationName,int[] rangeArr){
		if(start != null && end != null) {
			
			if(StringUtils.isNotEmpty(stationName) && StringUtils.isNotEmpty(deviceName)) {
				//日期、厂站、设备
				return faultRecordManager.findGe500kVFaultByDateAndDeviceAndStationForApp(principal,start, end, deviceName,stationName,rangeArr);
			}else if(StringUtils.isEmpty(stationName) && StringUtils.isEmpty(deviceName)) {
				//日期
				return faultRecordManager.findGe500kVFaultByTimedForApp(principal,start, end,rangeArr);
			}else if(StringUtils.isNotEmpty(stationName) && StringUtils.isEmpty(deviceName)) {
				//日期、厂站
				return faultRecordManager.findGe500kVFaultByDateAndStationForApp(principal,start, end, stationName,rangeArr);
			}else if(StringUtils.isEmpty(stationName) && StringUtils.isNotEmpty(deviceName)) {
				//日期、设备
				return faultRecordManager.findGe500kVFaultByDateAndDeviceForApp(principal,start, end, deviceName,rangeArr);
			}
		}else {
			if(StringUtils.isNotEmpty(stationName) && StringUtils.isNotEmpty(deviceName)) {
				//厂站、设备
				return faultRecordManager.findFaultsByStationNameAndDevice(principal,stationName, deviceName,rangeArr);
			}else if(StringUtils.isNotEmpty(stationName) && StringUtils.isEmpty(deviceName)) {
				//厂站
				return faultRecordManager.findFaultsByStationName(principal,stationName,rangeArr);
			}else if(StringUtils.isEmpty(stationName) && StringUtils.isNotEmpty(deviceName)) {
				//设备
				return faultRecordManager.findFaultsByDevice(principal,deviceName,rangeArr);
			}else if(StringUtils.isEmpty(stationName) && StringUtils.isEmpty(deviceName)) {
				//都为空
				return null;
			}
		}
		return null;
	} 

	/**
	 * 故障报告详情(List)
	 * 
	 * @param faultRecord
	 * @return
	 */
	public List<FaultReportDetailDTO> getFaultReportDetailList(String faultId) {

		FaultRecord faultRecord = dao.findById(faultId, FaultRecord.class);
		List<FaultReportDetailDTO> faultReportStationDetailDTOList = new ArrayList<FaultReportDetailDTO>();
		if (StringUtils.isNotEmpty(faultRecord.getStationId())) {
			// 所有的信息
			FaultReportDetailDTO faultReportStationDetailDTO = getFaultReportStationDetailDTO(faultRecord,
					faultRecord.getStationId(), faultRecord.getStationName());
			faultReportStationDetailDTOList.add(faultReportStationDetailDTO);
		}
		if (StringUtils.isNotEmpty(faultRecord.getStationId2())) {
			FaultReportDetailDTO faultReportStationDetailDTO2 = getFaultReportStationDetailDTO(faultRecord,
					faultRecord.getStationId2(), faultRecord.getStationName());
			faultReportStationDetailDTOList.add(faultReportStationDetailDTO2);
		}
		return faultReportStationDetailDTOList;
	}

	/**
	 * 故障报告详情(Map)
	 * 
	 * @param faultRecord
	 * @return
	 */
	public Map<String, Object> getFaultReportDetailMap(String faultId) {
		Map<String, Object> map = new HashMap<String, Object>();
		FaultRecord faultRecord = dao.findById(faultId, FaultRecord.class);
		if(faultRecord == null) {
			return map;
		}
		List<FaultReportDetailDTO> faultReportStationDetailDTOList = new ArrayList<FaultReportDetailDTO>();
		if (StringUtils.isNotEmpty(faultRecord.getStationId())) {
			// 所有的信息
			FaultReportDetailDTO faultReportStationDetailDTO = getFaultReportStationDetailDTO(faultRecord,
					faultRecord.getStationId(), faultRecord.getStationName());
			if(faultReportStationDetailDTO != null) {
				faultReportStationDetailDTOList.add(faultReportStationDetailDTO);
			}
		}
		if (StringUtils.isNotEmpty(faultRecord.getStationId2())) {
			FaultReportDetailDTO faultReportStationDetailDTO2 = getFaultReportStationDetailDTO(faultRecord,
					faultRecord.getStationId2(), faultRecord.getStationName());
			if(faultReportStationDetailDTO2 != null) {
				faultReportStationDetailDTOList.add(faultReportStationDetailDTO2);
			}
		}
		Map<String, Object> faultRecordDtoMap = new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(faultRecord.getId())) {
			faultRecordDtoMap.put("id", faultRecord.getId());
		}else {
			faultRecordDtoMap.put("id", "");
		}
		if(StringUtils.isNotEmpty(faultRecord.getEquipment())) {
			faultRecordDtoMap.put("equipment", faultRecord.getEquipment());
		}else {
			faultRecordDtoMap.put("equipment", "");
		}
		if(StringUtils.isNotEmpty(faultRecord.getEtype())) {
			faultRecordDtoMap.put("equipType", faultRecord.getEtype());
		}else {
			faultRecordDtoMap.put("equipType", "");
		}
		if(StringUtils.isNotEmpty(faultRecord.getFaultPhase())) {
			faultRecordDtoMap.put("faultPhase", faultRecord.getFaultPhase());
		}else {
			faultRecordDtoMap.put("faultPhase", "");
		}
		if(StringUtils.isNotEmpty(faultRecord.getFaultReson())) {
			faultRecordDtoMap.put("faultReason", faultRecord.getFaultReson());
		}else {
			faultRecordDtoMap.put("faultReason", "");
		}
		if(StringUtils.isNotEmpty(faultRecord.getFaultType())) {
			faultRecordDtoMap.put("faultType", faultRecord.getFaultType());
		}else {
			faultRecordDtoMap.put("faultType", "");
		}
		if(faultRecord.getFaultTime() != null) {
			Calendar calendar = Calendar.getInstance();
	        calendar.setTime(faultRecord.getFaultTime());
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			faultRecordDtoMap.put("faultTime", sdf.format(calendar.getTime()));
		}else {
			faultRecordDtoMap.put("faultTime", "");
		}
		map.put("faultRecord", faultRecordDtoMap);
		map.put("stationDetailList", faultReportStationDetailDTOList);
		return map;
	}

	/**
	 * 抽取故障列表所需的数据 List<FaultListDTO>
	 * 
	 * @param faultRecords
	 * @return
	 */
	private List<FaultListDTO> getFaultLists(List<FaultRecord> faultRecords) {
		List<FaultListDTO> faultListDTOList = new ArrayList<FaultListDTO>();
		if (faultRecords!=null && faultRecords.size() > 0) {
			Iterator<FaultRecord> iterator = faultRecords.iterator();
			while (iterator.hasNext()) {
				FaultRecord faultRecord = iterator.next();
				FaultListDTO faultListDTO = getFaultListDTO(faultRecord);
				faultListDTOList.add(faultListDTO);
			}
		} 
		return faultListDTOList;
	}

	/**
	 * 根据faultRecord获取故障列表单条数据 FaultListDTO
	 * 
	 * @param faultRecord
	 * @return
	 */
	private FaultListDTO getFaultListDTO(FaultRecord faultRecord) {
		// 数据载体
		FaultListDTO faultListDTO = new FaultListDTO();
		faultListDTO.setFaultId(faultRecord.getId());
		faultListDTO.setDeviceType(faultRecord.getEtype());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(faultRecord.getFaultTime());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		faultListDTO.setFaultTime(sdf.format(calendar.getTime()));
		faultListDTO.setFaultDevice(faultRecord.getEquipment());
		faultListDTO.setFaultPhase(faultRecord.getFaultPhase());
		faultListDTO.setFaultType(faultRecord.getFaultType());
		faultListDTO.setFaultDeviceId(faultRecord.getEquipment_id());
		if (StringUtils.isNotEmpty(faultRecord.getStationId())) {
			// 获取变电站的所有的信息
			FaultReportDetailDTO faultReportStationDetailDTO = getFaultReportStationDetailDTO(faultRecord,
					faultRecord.getStationId(), faultRecord.getStationName());
			if (faultReportStationDetailDTO == null) {
				faultListDTO.setStationIdA(faultRecord.getStationId());
				faultListDTO.setStationA(faultRecord.getStationName());
			} else {
				// 抽取部分信息
				getFaultListDTO(faultRecord, faultReportStationDetailDTO, faultListDTO, 1);
			}
		}
		if (StringUtils.isNotEmpty(faultRecord.getStationId2())) {
			FaultReportDetailDTO faultReportStationDetailDTO2 = getFaultReportStationDetailDTO(faultRecord,
					faultRecord.getStationId2(), faultRecord.getStationName2());
			if (faultReportStationDetailDTO2 == null) {
				faultListDTO.setStationIdB(faultRecord.getStationId2());
				faultListDTO.setStationB(faultRecord.getStationName2());
			} else {
				getFaultListDTO(faultRecord, faultReportStationDetailDTO2, faultListDTO, 2);
			}
		}
		return faultListDTO;
	}

	/**
	 * 根据故障id和变电站id抽取变电站故障详情 FaultReportStationDetailDTO
	 * 
	 * @param faultId 故障id
	 * @param stationId 变电站id
	 * @param stationName 变电站名
	 * @return
	 */
	private FaultReportDetailDTO getFaultReportStationDetailDTO(FaultRecord faultRecord, String stationId,
			String stationName) {
		if (!StringUtils.isNotEmpty(stationId) || !StringUtils.isNotEmpty(faultRecord.getId())) {
			return null;
		}
		String faultId = faultRecord.getId();
		// 单站详情
		FaultReportStationDetail faultReportStationDetail = faultReportStationDetailManager
				.getFaultReportStationDetailById(faultId, stationId);
		if (faultReportStationDetail == null) {
			return null;
		}
		// 单站故障前后电流变化
		List<FaultDetailDTO> faultCurrentList = new ArrayList<FaultDetailDTO>();
		// 单站故障前后电压变化
		List<FaultDetailDTO> faultVoltageList = new ArrayList<FaultDetailDTO>();
		// 单站差动电流
		List<FaultDetailDTO> diffCurrentList = new ArrayList<FaultDetailDTO>();
		try {
			ProtectionActionAnalysisModel pmodel = AnalysisModelFactoryBuilder.getAnalysisModelFactory(faultRecord.getEtype()).makeAnalysisReport(faultRecord.getEquipment_id(), faultRecord.getFaultTime());
			if(pmodel.getFirstSt() != null && StringUtils.isNotBlank(pmodel.getFirstSt().getSubstaionId())) {
				//第一变电站
				if(pmodel.getFirstSt().getSubstaionId().equals(stationId) || pmodel.getFirstSt().getSubstationName().equals(stationName)) {
					faultCurrentList = pmodel.getFirstSt().getFaultCurrentDetailList();
					faultVoltageList = pmodel.getFirstSt().getFaultVoltageDetailList();
					diffCurrentList = pmodel.getDiffCurrentDetailList();
				}
			}
			if(pmodel.getSecondSt() != null && StringUtils.isNotBlank(pmodel.getSecondSt().getSubstaionId())) {
				//第二变电站
				if(pmodel.getSecondSt().getSubstaionId().equals(stationId) || pmodel.getSecondSt().getSubstationName().equals(stationName)) {
					faultCurrentList = pmodel.getSecondSt().getFaultCurrentDetailList();
					faultVoltageList = pmodel.getSecondSt().getFaultVoltageDetailList();
					diffCurrentList = pmodel.getDiffCurrentDetailList();
				}
			}
		} catch (Exception e1) {

			log.error("---->获取故障前后电压、电流、差动电流接口出现异常!");
			e1.printStackTrace();
		}
//		// 单站故障前后电流变化
//		List<FaultCharacteristicInfo> faultCurrentList = faultCharacteristicInfoManager
//				.getFaultCharacteristicInfoByCondition(stationId, faultId, "1");
//		// 单站故障前后电压变化
//		List<FaultCharacteristicInfo> faultVoltageList = faultCharacteristicInfoManager
//				.getFaultCharacteristicInfoByCondition(stationId, faultId, "2");
//		// 单站差动电流
//		List<FaultCharacteristicInfo> diffCurrentList = faultCharacteristicInfoManager
//				.getFaultCharacteristicInfoByCondition(stationId, faultId, "3");
//		
		// 查询故障录波列表
		List<String[]> waveList = getWaveFileList(faultRecord, stationName);
		// 抽取FaultReportStationDetailDTO数据
		FaultReportStationDetailDTO faultReportStationDetailDTO = new FaultReportStationDetailDTO();
		faultReportStationDetailDTO.setFaultFileId(faultReportStationDetail.getFaultFileId());
		faultReportStationDetailDTO.setStationId(faultReportStationDetail.getStationId());
		faultReportStationDetailDTO.setStationName(faultReportStationDetail.getStationName());
		faultReportStationDetailDTO.setFirstFault(faultReportStationDetail.getFirstFault());
		faultReportStationDetailDTO.setFirstElectricity(faultReportStationDetail.getFirstElectricity());
		faultReportStationDetailDTO.setSecondElectricity(faultReportStationDetail.getSecondElectricity());
		faultReportStationDetailDTO.setReclosingSuccess(faultReportStationDetail.getReclosingSuccess());
		faultReportStationDetailDTO.setFaultRangingFirst(faultReportStationDetail.getFaultRangingFirst());
		faultReportStationDetailDTO.setManualFaultRangingFirst(faultReportStationDetail.getManualFaultRangingFirst());
		faultReportStationDetailDTO.setFaultRangingSecond(faultReportStationDetail.getFaultRangingSecond());
		faultReportStationDetailDTO.setManualFaultRangingSecond(faultReportStationDetail.getManualFaultRangingSecond());
		faultReportStationDetailDTO.setFaultRangingThird(faultReportStationDetail.getFaultRangingThird());
		faultReportStationDetailDTO.setManualFaultRangingThird(faultReportStationDetail.getManualFaultRangingThird());
		faultReportStationDetailDTO.setRangingWave(faultReportStationDetail.getRangingWave());
		faultReportStationDetailDTO.setManualRangingWave(faultReportStationDetail.getManualRangingWave());
		
	    Set<FaultReportComponentDetailDTO> protectActListDTOs = new HashSet<FaultReportComponentDetailDTO>();
		List<FaultCharacteristicInfoDTO> faultCurrentListDTOs = new ArrayList<FaultCharacteristicInfoDTO>();
		for (FaultDetailDTO frcd : faultCurrentList) {
			FaultCharacteristicInfoDTO fciDto = new FaultCharacteristicInfoDTO();
			try {
				BeanUtils.copyProperties(fciDto, frcd);
			} catch (IllegalAccessException | InvocationTargetException e) {
				log.error("故障前后电流:执行BeanUtils.copyProperties()时出现异常!");
				e.printStackTrace();
			}
			faultCurrentListDTOs.add(fciDto);
		}
		List<FaultCharacteristicInfoDTO> faultVoltageListDTOs = new ArrayList<FaultCharacteristicInfoDTO>();
		for (FaultDetailDTO frcd : faultVoltageList) {
			FaultCharacteristicInfoDTO fciDto = new FaultCharacteristicInfoDTO();
			try {
				BeanUtils.copyProperties(fciDto, frcd);
			} catch (IllegalAccessException | InvocationTargetException e) {
				log.error("故障前后电压:执行BeanUtils.copyProperties()时出现异常!");
				e.printStackTrace();
			}
			faultVoltageListDTOs.add(fciDto);
		}
		List<FaultCharacteristicInfoDTO> diffCurrentListDTOs = new ArrayList<FaultCharacteristicInfoDTO>();
		for (FaultDetailDTO frcd : diffCurrentList) {
			FaultCharacteristicInfoDTO fciDto = new FaultCharacteristicInfoDTO();
			try {
				BeanUtils.copyProperties(fciDto, frcd);
			} catch (IllegalAccessException | InvocationTargetException e) {
				log.error("差动电流:执行BeanUtils.copyProperties()时出现异常!");
				e.printStackTrace();
			}
			diffCurrentListDTOs.add(fciDto);
		}
		
		Set<FaultReportComponentDetail> faultReportComponentDetails = faultReportStationDetail.getFaultReportComponentDetails();
		for (FaultReportComponentDetail frcd : faultReportComponentDetails) {
			FaultReportComponentDetailDTO frcdDto = new FaultReportComponentDetailDTO();
			try {
				BeanUtils.copyProperties(frcdDto, frcd);
			} catch (IllegalAccessException | InvocationTargetException e) {
				log.error("动作元件:执行BeanUtils.copyProperties()时出现异常!");
				e.printStackTrace();
			}
			protectActListDTOs.add(frcdDto);
		}
		
		faultReportStationDetailDTO.setProtectActList(protectActListDTOs);
		faultReportStationDetailDTO.setDiffCurrentList(diffCurrentListDTOs);
		faultReportStationDetailDTO.setFaultCurrentList(faultCurrentListDTOs);
		faultReportStationDetailDTO.setFaultVoltageList(faultVoltageListDTOs);
		faultReportStationDetailDTO.setWaveFiles(waveList);
		
		FaultReportDetailDTO faultReportDetailDTO = new FaultReportDetailDTO();
		faultReportDetailDTO.setFaultReportStationDetail(faultReportStationDetailDTO);
		faultReportDetailDTO.setProtectActList(protectActListDTOs);
		faultReportDetailDTO.setDiffCurrentList(diffCurrentListDTOs);
		faultReportDetailDTO.setFaultCurrentList(faultCurrentListDTOs);
		faultReportDetailDTO.setFaultVoltageList(faultVoltageListDTOs);
		faultReportDetailDTO.setWaveFiles(waveList);
		
		return faultReportDetailDTO;
	}

	/**
	 * 使用faultListDTO来保存同一故障两个变电站的数据
	 * 
	 * @param faultRecord 故障记录
	 * @param faultReportStationDetailDTO 单站故障数据
	 * @param faultListDTO 故障列表数据
	 * @param stationStatus      1:本侧变电站2：对侧变电站
	 * @return
	 */
	private void getFaultListDTO(FaultRecord faultRecord, FaultReportDetailDTO faultReportStationDetailDTO,
			FaultListDTO faultListDTO, int stationStatus) {
		// 重合闸是否成功
		faultListDTO.setRecloseSuccess(faultReportStationDetailDTO.getFaultReportStationDetail().getReclosingSuccess());
		if (stationStatus == 1) {
			// 本侧厂站			
			if(StringUtils.isNotEmpty(faultReportStationDetailDTO.getFaultReportStationDetail().getStationName())) {
				faultListDTO.setStationIdA(faultReportStationDetailDTO.getFaultReportStationDetail().getStationId());
				faultListDTO.setStationA(faultReportStationDetailDTO.getFaultReportStationDetail().getStationName());
			}else {
				faultListDTO.setStationIdA(faultRecord.getStationId());
				faultListDTO.setStationA(faultRecord.getStationName());
			}			
			// 本侧录波测距
			faultListDTO.setRecorderRangeA(faultReportStationDetailDTO.getFaultReportStationDetail().getRangingWave());
			// 本侧故障持续时间
			faultListDTO.setLastTimeA(faultReportStationDetailDTO.getFaultReportStationDetail().getFirstFault());
			// 本侧录波文件列表
			faultListDTO.setWaveFilesA(faultReportStationDetailDTO.getWaveFiles());
		} else if (stationStatus == 2) {
			// 对侧厂站
			if(StringUtils.isNotEmpty(faultReportStationDetailDTO.getFaultReportStationDetail().getStationName())) {
				faultListDTO.setStationIdB(faultReportStationDetailDTO.getFaultReportStationDetail().getStationId());
				faultListDTO.setStationB(faultReportStationDetailDTO.getFaultReportStationDetail().getStationName());
			}else {
				faultListDTO.setStationIdB(faultRecord.getStationId2());
				faultListDTO.setStationB(faultRecord.getStationName2());
			}	
			// 对侧录波测距
			faultListDTO.setRecorderRangeB(faultReportStationDetailDTO.getFaultReportStationDetail().getRangingWave());
			// 对侧故障持续时间
			faultListDTO.setLastTimeB(faultReportStationDetailDTO.getFaultReportStationDetail().getFirstFault());
			// 对侧录波文件列表
			faultListDTO.setWaveFilesB(faultReportStationDetailDTO.getFaultReportStationDetail().getWaveFiles());
		}
	}

	/**
	 * 获取录播文件列表[id type]
	 * 
	 * @param faultRecord
	 * @param stationName 若变电站名为null，则返回整个故障的录波文件
	 * @return
	 */
	public List<String[]> getWaveFileList(FaultRecord fault, String stationName) {
		List<AbstractWaveFile> waveFiles = relationManager.unionListWaveFiles(fault.getId(), stationName);
		if (waveFiles == null || !(waveFiles.size() > 0)) {
			return null;
		}
		List<String[]> WaveList = new ArrayList<>();
		for (AbstractWaveFile wave : waveFiles) {
			String[] s = null;
			if (wave instanceof WaveFile) {
				s = new String[] { wave.getId(), "集中录波" };
			} else if (wave instanceof ProtectWaveFile) {
				// 截取断路器保护名字
				String type = wave.getEquipType();
				if ("断路器保护".equals(wave.getEquipType())) {
					type = ProtectionNwUtil.getBreakerOfBreakerProtect(wave.getEquipment()) + "保护";
				}
				s = new String[] { wave.getId(), type };
			}
			WaveList.add(s);
		}
		return WaveList;
	}

	/**
	 * 获取身份信息
	 * @param userCode
	 * @return
	 */
	private Principal getPrincipal(String userCode){
		Principal principal = new Principal();
		PersonnelEntityBean person = pm.findPersonnelByCode(userCode);
		Set<RoleEntityBean> roles = person.getRoles();
		for(RoleEntityBean role:roles){
			if(RoleTypeEnum.system.toString().equals(role.getType())){
				principal.setSystemManager(true);
				return principal;
			}
		}
		List<DepartmentEntityBean> list = dm.recursiveFindParentDepartments(person);
		for(DepartmentEntityBean depart:list){
			principal.getDepartIds().add(depart.getId());
		}
		return principal;
	}
}
